package mspbots.data.tl.prod.datateams.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import mspbots.data.tl.base.AbstractBaseServiceImpl;
import mspbots.data.tl.prod.datateams.mapper.TeamsPlannersTasksMapper;
import mspbots.data.tl.prod.datateams.model.ResultMsg;
import mspbots.data.tl.prod.datateams.model.TeamsPlannersTasks;
import mspbots.data.tl.prod.datateams.model.TeamsPlannersTasksTemp;
import mspbots.data.tl.prod.datateams.service.TeamsPlannersTasksService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * @author Jay.Yang
 */

@Service
@Log4j2
@DS("master")
@RequiredArgsConstructor
public class TeamsPlannersTasksServiceImpl extends AbstractBaseServiceImpl<TeamsPlannersTasksMapper, TeamsPlannersTasks>
        implements TeamsPlannersTasksService {

    @Override
    public void sync(ResultMsg resultMsg) {
        JsonNode entity = resultMsg.getEntity();
        TeamsPlannersTasksTemp temp = super.objectMapper.convertValue(entity, TeamsPlannersTasksTemp.class);
        TeamsPlannersTasks data = TeamsPlannersTasks.builder().build();
        BeanUtils.copyProperties(temp,data);
        data.setTaskId(resultMsg.getId());
        data.setTenantCode(resultMsg.getTenantId());
        data.setSyncTime(resultMsg.getSyncDate());

        data.setOdataEtag(super.getStringValue(entity.get("@odata.etag")));
        if (!ObjectUtils.isEmpty(entity.get("createdBy")) && !ObjectUtils.isEmpty(entity.get("createdBy").get("user"))){
            data.setCreatedByUserId(super.getStringValue(entity.get("createdBy").get("user").get("id")));
            data.setCreatedByUserDisplayName(super.getStringValue(entity.get("createdBy").get("user").get("displayName")));
        }
        if (!ObjectUtils.isEmpty(entity.get("completedBy")) && !ObjectUtils.isEmpty(entity.get("completedBy").get("user"))){
            data.setCompletedByUserId(super.getStringValue(entity.get("completedBy").get("user").get("id")));
            data.setCompletedByUserDisplayName(super.getStringValue(entity.get("completedBy").get("user").get("displayName")));
        }

        JsonNode plans = entity.get("plans");
        if (!ObjectUtils.isEmpty(plans)){
            data.setPlanOwner(super.getStringValue(plans.get("owner")));
            data.setPlanTitle(super.getStringValue(plans.get("title")));
            data.setPlanOdataEtag(super.getStringValue(plans.get("@odata.etag")));
            data.setPlanCreatedDateTime(super.getDateTimeValue(plans.get("createdDateTime")));
            JsonNode createdBy = plans.get("createdBy");
            if (!ObjectUtils.isEmpty(createdBy)){
                if (!ObjectUtils.isEmpty(createdBy.get("user"))){
                    data.setPlanCreatedByUserId(super.getStringValue(createdBy.get("user").get("id")));
                    data.setPlanCreatedByUserDisplayName(super.getStringValue(createdBy.get("user").get("displayName")));
                }
                if (!ObjectUtils.isEmpty(createdBy.get("application"))){
                    data.setPlanCreatedByApplicationId(super.getStringValue(createdBy.get("application").get("id")));
                    data.setPlanCreatedByApplicationDisplayName(super.getStringValue(createdBy.get("application").get("displayName")));
                }
            }
        }

        TeamsPlannersTasks dbData = super.baseMapper.selectOne(Wrappers.<TeamsPlannersTasks>lambdaQuery()
                .eq(TeamsPlannersTasks::getTenantCode, data.getTenantCode())
                .eq(TeamsPlannersTasks::getTaskId, data.getTaskId())
                .last("limit 1"));
        if (ObjectUtils.isEmpty(dbData)){
            super.baseMapper.insert(data);
        }else{
            data.setId(dbData.getId());
            super.baseMapper.updateById(data);
        }
    }
}
